<?php
/**
 * File: $Id: xarversion.php 1.20 04/07/18 21:48:25+03:00 voll@yahoo.com $
 *
 * lilina
 *
 * @package lilina
 * @copyright (C) 2003 by the Xaraya Development Team.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage base
 * @author Panayotis Vryonis <panayotis@vrypan.net>
 * @author John Cox <admin@dinerminor.com>
*/

// Load Table Maintaince API
xarDBLoadTableMaintenanceAPI();

/**
 * Initialise the headlines module
 *
 * @return bool
 * @raise DATABASE_ERROR
 */
function lilina_init()
{
    // Get database information
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    // Create tables
    $table = $xartable['feeds'];

    $query = xarDBCreateTable($table,
                             array('xar_id'         => array('type'        => 'integer',
                                                             'null'        => false,
                                                             'default'     => '0',
                                                             'increment'   => true,
                                                             'primary_key' => true),
                                   'xar_url'        => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),
                                   'xar_keyword'    => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => '')));
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $gtable = $xartable['garbage'];

    $query = xarDBCreateTable($gtable,
                             array('xar_id'        => array('type'        => 'integer',
                                                             'null'        => false,
                                                             'default'     => '0',
                                                             'increment'   => true,
                                                             'primary_key' => true),
                                   'xar_unique'   => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => '')));
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $stable = $xartable['story'];

    $query = xarDBCreateTable($stable,
                             array('xar_id'        => array('type'        => 'integer',
                                                             'null'        => false,
                                                             'default'     => '0',
                                                             'increment'   => true,
                                                             'primary_key' => true),
                                   'xar_title'      => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),
                                   'xar_summary'    => array('type'        => 'text',
                                                             'default'     => ''),
                                   'xar_link'       => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),
                                   'xar_timestamp'  => array('type'        => 'integer',
                                                             'null'        => false,
                                                             'default'     => '0',
                                                             'increment'   => false,
                                                             'primary_key' => false),
                                   'xar_ctitle'     => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),
                                   'xar_curl'       => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),    
                                   'xar_cfavicon'   => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),
                                   'xar_keyword'    => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => ''),
                                   'xar_unique'     => array('type'        => 'varchar',
                                                             'size'        => 255,
                                                             'null'        => false,
                                                             'default'     => '')));
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $table . '_url',
                   'fields'    => array('xar_url'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($table,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $table . '_keyword',
                   'fields'    => array('xar_keyword'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($table,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $gtable . '_unique',
                   'fields'    => array('xar_unique'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($gtable,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $stable . '_title',
                   'fields'    => array('xar_title'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($stable,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $stable . '_unique',
                   'fields'    => array('xar_unique'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($stable,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $stable . '_keyword',
                   'fields'    => array('xar_keyword'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($stable,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    $index = array('name'      => 'i_' . $stable . '_timestamp',
                   'fields'    => array('xar_timestamp'),
                   'unique'    => FALSE);
    $query = xarDBCreateIndex($stable,$index);
    $result =& $dbconn->Execute($query);
    if (!$result) return;

    // Set up module variables
    xarModSetVar('lilina', 'itemsperpage', 50);

    // Register Masks
    xarRegisterMask('OverviewLilina','All','lilina','All','All','ACCESS_OVERVIEW');
    xarRegisterMask('AdminLilina','All','lilina','All','All','ACCESS_ADMIN');

    // search hook
    if (!xarModRegisterHook('item', 'search', 'GUI', 'lilina', 'user', 'search')) return false;
    return true;
}

function lilina_upgrade($oldVersion)
{
    // Upgrade dependent on old version number
    switch($oldVersion) {
    case '0.9.0':
    // Get database information
        $dbconn =& xarDBGetConn();
        $xartable =& xarDBGetTables();
        $gtable = $xartable['garbage'];
        $query = xarDBCreateTable($gtable,
                                 array('xar_id'        => array('type'        => 'integer',
                                                                 'null'        => false,
                                                                 'default'     => '0',
                                                                 'increment'   => true,
                                                                 'primary_key' => true),
                                       'xar_unique'   => array('type'        => 'varchar',
                                                                 'size'        => 255,
                                                                 'null'        => false,
                                                                 'default'     => '')));
        $result =& $dbconn->Execute($query);
        if (!$result) return;
        $index = array('name'      => 'i_' . $gtable . '_unique',
                       'fields'    => array('xar_unique'),
                       'unique'    => FALSE);
        $query = xarDBCreateIndex($gtable,$index);
        $result =& $dbconn->Execute($query);
        if (!$result) return;
        break;
    case '0.9.1':
    // Get database information
        $dbconn =& xarDBGetConn();
        $xartable =& xarDBGetTables();
        $stable = $xartable['story'];
        $query = xarDBAlterTable($stable,
                          array('command' => 'add',
                                'field'   => 'xar_keyword',
                                'type'    => 'varchar',
                                 'size'        => 255,
                                 'null'        => false,
                                 'default'     => ''));
        $result =& $dbconn->Execute($query);
        if (!$result) return;
        $index = array('name'      => 'i_' . $stable . '_keyword',
                       'fields'    => array('xar_keyword'),
                       'unique'    => FALSE);
        $query = xarDBCreateIndex($stable,$index);
        $result =& $dbconn->Execute($query);
        if (!$result) return;
        break;
    }
    // Update successful
    return true;
}


/**
 * Delete the headlines module
 *
 * @returns bool
 */
function lilina_delete()
{
    // need to drop the module tables too
    // Get database information
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();

    // Generate the SQL to drop the table using the API
    $query = xarDBDropTable($xartable['feeds']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    $query = xarDBDropTable($xartable['story']);
    if (empty($query)) return; // throw back
    // Drop the table and send exception if returns false.
    $result =& $dbconn->Execute($query);
    if (!$result) return;
    xarModDelAllVars('lilina');
    xarRemoveMasks('lilina');
    xarRemoveInstances('lilina');
    return true;
}
?>